

package com.hazelcast.test.starter.answer;

import com.hazelcast.cluster.Address;
import com.hazelcast.internal.serialization.Data;
import org.mockito.invocation.InvocationOnMock;

import javax.cache.expiry.ExpiryPolicy;
import java.lang.reflect.Method;
import java.util.Collection;

import static java.util.Arrays.copyOf;

/**
 * Default {@link org.mockito.stubbing.Answer} to create a mock for a proxied
 * {@link com.hazelcast.map.impl.recordstore.RecordStore} or
 * {@link com.hazelcast.cache.impl.ICacheRecordStore}.
 */
class RecordStoreAnswer extends AbstractAnswer {

    private final Class<?> delegateDataClass;
    private final Class<?> delegateAddressClass;
    private final Class<?> delegateExpiryPolicyClass;

    RecordStoreAnswer(Object delegate) throws Exception {
        super(delegate);
        delegateDataClass = delegateClassloader.loadClass(Data.class.getName());
        delegateAddressClass = delegateClassloader.loadClass(Address.class.getName());
        delegateExpiryPolicyClass = delegateClassloader.loadClass(ExpiryPolicy.class.getName());
    }

    @Override
    Object answer(InvocationOnMock invocation, String methodName, Object[] arguments) throws Exception {
        if (arguments.length == 3 && methodName.equals("get")) {
            // RecordStore
            return getMapValue(methodName, arguments);
        } else
            if (arguments.length == 3 && methodName.equals("setExpiryPolicy")) {
                // ICacheRecordStore
                return setExpiryPolicy(invocation, methodName, arguments);
            } else
                if (arguments.length == 2 && methodName.equals("get")) {
                    // ICacheRecordStore
                    return getCacheValue(methodName, arguments);
                } else
                    if (arguments.length == 0) {
                        switch (methodName) {
                        case "beforeOperation":
                        case "afterOperation":
                            return null;
                        case "size":
                        case "getReadOnlyRecords":
                            return invoke(invocation);
                        }
                    }
        throw new UnsupportedOperationException("Method is not implemented in RecordStoreAnswer: " + methodName);
    }

    private Object getMapValue(String methodName, Object[] arguments) throws Exception {
        // the RecordStore.get() method changed its signature between 3.10 and 3.11
        try {
            Method delegateMethod = getDelegateMethod(methodName, delegateDataClass, Boolean.TYPE, delegateAddressClass);
            return invoke(delegateMethod, arguments);
        } catch (NoSuchMethodException e) {
            Method delegateMethod = getDelegateMethod(methodName, delegateDataClass, Boolean.TYPE);
            return invoke(delegateMethod, arguments[0], arguments[1]);
        }
    }

    private Object getCacheValue(String methodName, Object[] arguments) throws Exception {
        Method delegateMethod = getDelegateMethod(methodName, delegateDataClass, delegateExpiryPolicyClass);
        return invoke(delegateMethod, arguments);
    }

    private Object setExpiryPolicy(InvocationOnMock invocation, String methodName, Object[] arguments) throws Exception {
        // the ICacheRecordStore.setExpiryPolicy() method changed its signature between 3.10 and 3.11
        try {
            return invoke(invocation, arguments);
        } catch (NoSuchMethodException e) {
            Method delegateMethod = getDelegateMethod(methodName, Collection.class, Object.class, String.class, Integer.TYPE);
            Object[] legacyArguments = copyOf(arguments, 4);
            legacyArguments[3] = 0;
            return invoke(delegateMethod, legacyArguments);
        }
    }
}
